import { getToken } from '@/utils/authStore.js'
import request from '@/utils/request.js'
import store from '@/store/index.js'
import Qs from 'qs'


/**
 * 获取权限表
 */

const getAppConfigsApi = () => {

  return new Promise((resolve, reject) => {
    if (!getToken()) {
      reject()
    }
    setTimeout(() => {
      resolve(['1', '2'])
    }, 500)
  })
}


/**
 *  登录获取token
 * @param {Object} param0
 */
const getTokenByLogin = ({ username, password }) => {
  let defaultConfig = {
    grant_type: 'password',
    client_id: 'back',
    client_secret: '123456',
    domain: 'b',
    authenticationType: 'password'
  }

  return request({
    method: 'post',
    url: '/uua/oauth/token',
    data: Qs.stringify(Object.assign({ username, password }, defaultConfig))
  })
}
/**
 * 获取用户信息
 */
const getUserInfos = () => {
  if (store.getters.token) {
    let token = store.getters.token.split(' ')[1];
    return request({ method: 'post', url: '/uua/oauth/check_token', data: Qs.stringify({ token }) })
  }
}

const getWebSite = () => request({method:'get',url:'/cms/dict/findbydictname?dictName=%E7%AB%99%E7%82%B9%E8%AE%BE%E7%BD%AE'})

const getUserInfo = () => {
  return new Promise((resolve) => {
    let userInfo = {
      user_authorities: {
        permissionCodeList: ['1', '2']
      }
    }
    resolve(userInfo)
  })
}

export {
  getTokenByLogin,
  getAppConfigsApi,
  getUserInfos,
  getUserInfo,
  getWebSite
}
